package com.SocketDemo.TCPTest;

/*
 * 接收数据，给出反馈
 * */

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        //创建ServerSocket对象
        ServerSocket ss = new ServerSocket(10000);

        //监听客户端数据
        Socket s = ss.accept();

        //输入流接收数据
//        InputStream is = s.getInputStream();
//        byte[] bys = new byte[1024];
//        int len;
//        while ((len = is.read(bys)) != 0) {
//            String data = new String(bys, 0, len);
//            System.out.println(data);
//        }

        //接收数据
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //把数据写入文本文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/caoting/IdeaProjects/untitled/ss01.txt"));
        String line;
        while((line = br.readLine())!=null){//等待读取数据
//            System.out.println(line);
            bw.write(line);
            bw.newLine();
            bw.flush();
        }



        //给出反馈
//        OutputStream os = s.getOutputStream();
//        os.write("发送数据成功".getBytes(StandardCharsets.UTF_8));
        BufferedWriter bwserver = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bwserver.write("数据发送完成");
        bwserver.newLine();
        bwserver.flush();

        //释放资源
        bwserver.close();
        bw.close();
        ss.close();

    }
}
